import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class TestDeployment:
    url_unify = "/clusters/host"

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/deployment/1_get_deployment_list.yaml'))
    def test_get_deployment_list(self, deploymentinfo):
        # 获取deployment列表接口-正常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = base_url + deploymentinfo['request']['url'].replace('{namespace}', 'default')
        method = deploymentinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/deployment/1_get_deployment_list_exception.yaml'))
    def test_get_deployment_list_exception(self, deploymentinfo):
        # 获取deployment列表接口-异常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = base_url + deploymentinfo['request']['url'].replace('{namespace}', 'default')
        method = deploymentinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/deployment/2_create_deployment.yaml')
                             )
    def test_create_deployment(self, deploymentinfo):
        # 创建deployment接口-正常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = base_url + deploymentinfo['request']['url'].replace('{namespace}', 'default')
        method = deploymentinfo['request']['method']
        data = deploymentinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        deploymentname = req.json()['metadata']['name']
        globals()['deploymentname'] = deploymentname
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/deployment/2_create_deployment_exception.yaml'))
    def test_create_deployment_exception(self, deploymentinfo):
        # 创建deployment接口-异常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = base_url + deploymentinfo['request']['url'].replace('{namespace}', 'default')
        method = deploymentinfo['request']['method']
        data = deploymentinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/deployment/3_get_specific_deployment.yaml'))
    def test_get_specific_deployment(self, deploymentinfo):
        # 获取指定deployment接口-正常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = (base_url + deploymentinfo['request']['url']
               .replace('{namespace}', 'default').replace('{deploymentname}', globals()['deploymentname']))
        method = deploymentinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/deployment/3_get_specific_deployment_exception.yaml'))
    def test_get_specific_deployment_exception(self, deploymentinfo):
        # 获取指定deployment接口-异常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = (base_url + deploymentinfo['request']['url']
               .replace('{namespace}', 'default').replace('{deploymentname}', globals()['deploymentname']))
        method = deploymentinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo',
                             YamlUtil().read_testcase_yaml('v25.06/resourcemanage/deployment/4_update_deployment.yaml')
                             )
    def test_update_deployment(self, deploymentinfo):
        # 更新指定deployment接口-正常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = (base_url + deploymentinfo['request']['url']
               .replace('{namespace}', 'default')
               .replace('{deploymentname}', globals()['deploymentname']))
        method = deploymentinfo['request']['method']
        data = deploymentinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/deployment/4_update_deployment_exception.yaml'))
    def test_update_deployment_exception(self, deploymentinfo):
        # 更新指定deployment接口-异常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = (base_url + deploymentinfo['request']['url']
               .replace('{namespace}', 'default')
               .replace('{deploymentname}', globals()['deploymentname']))
        method = deploymentinfo['request']['method']
        data = deploymentinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/deployment/5_update_section_deployment.yaml'))
    def test_update_section_deployment(self, deploymentinfo):
        # 部分更新指定deployment的接口-正常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = (base_url + deploymentinfo['request']['url']
               .replace('{namespace}', 'default').replace('{deploymentname}', globals()['deploymentname']))
        method = deploymentinfo['request']['method']
        data = deploymentinfo['request']['data']
        header = deploymentinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/deployment/5_update_section_deployment_exception.yaml'))
    def test_update_section_deployment_exception(self, deploymentinfo):
        # 部分更新指定deployment的接口-异常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = (base_url + deploymentinfo['request']['url']
               .replace('{namespace}', 'default').replace('{deploymentname}', globals()['deploymentname']))
        method = deploymentinfo['request']['method']
        data = deploymentinfo['request']['data']
        header = deploymentinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header, json=data)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/deployment/6_delete_specific_deployment.yaml'))
    def test_delete_specific_deployment(self, deploymentinfo):
        # 删除deployment接口-正常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = (base_url + deploymentinfo['request']['url']
               .replace('{namespace}', 'default').replace('{deploymentname}', globals()['deploymentname']))
        method = deploymentinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.k8sresource
    @pytest.mark.deployment
    @pytest.mark.parametrize('deploymentinfo', YamlUtil().read_testcase_yaml(
        'v25.06/resourcemanage/deployment/6_delete_specific_deployment_exception.yaml'))
    def test_delete_specific_deployment_exception(self, deploymentinfo):
        # 删除deployment接口-异常
        base_url = datas.baseUrl + TestDeployment.url_unify
        url = (base_url + deploymentinfo['request']['url']
               .replace('{namespace}', 'default').replace('{deploymentname}', globals()['deploymentname']))
        method = deploymentinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == deploymentinfo['validate'][0]['equals']['status_code']
